<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>

  import echarts from 'echarts';
  require('echarts/theme/macarons');
  import resize from '@/views/dashboard/mixins/resize';
  import { getProjectHundredChaptersAccumulateChartLine } from "@/api/engineering/summary";

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '450px'
      },
      projectId: {
        type: Number,
        required: true
      }
    },
    data() {
      return {
        chart: null,
        data: [],
        legend: [],
      }
    },
    watch: {
      projectId: {
        deep: true,
        handler(val) {
          this.projectId = val;
          this.setOptions();
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      console.log("LineChart: beforeDestroy");
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        console.log("LineChart: initChart");
        this.setOptions();
      },
      setOptions() {
        console.log("LineChart-----项目ID：" + this.projectId);
        // 加载数据
        getProjectHundredChaptersAccumulateChartLine(this.projectId).then(response => {
          this.chart = echarts.init(this.$el, 'macarons');
          let option = {
            title: {
              text: '百章每期累计金额完成统计',
              subtext: '单位（万元）',
              left: 'center'
            },
            tooltip: {
              trigger: 'item'
            },
            legend: {
              top: 50,
              data: response.axis
            },
            grid: {
              top: 90,
              show: false
            },
            xAxis: {
              // 去除x轴的背景网格
              splitLine: {
                show: false,
              },
              type: 'category',
              boundaryGap: false,
              data: response.periods
            },
            yAxis: {
              splitLine: {
                show: false,
              },
              type: 'value',
              axisLabel: {
                formatter: '{value}'
              }
            },
            series: response.lines
          };
          this.chart.setOption(option);
          if (response.lines.length == 0) {
            this.chart.showLoading({
              text: '暂无数据',
              showSpinner: false,
              textColor: '#808080',
              maskColor: 'rgba(255, 255, 255, 1)',
              fontSize: '26px',
              fontWeight: 'bold'
            })
          } else {
            this.chart.hideLoading();
          }
        });
      }
    }
  }

</script>
